import { RebateModel } from '../../../../models/rebate'

const rebateModel = new RebateModel()

import { MineModel } from '../../../../models/mine'

const mineModel = new MineModel()

const App = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    vipUserRebate: null,
    brokerage: 0,
    phone: '',
    ifName: false,
    code: '',
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar:false,
    Income: {} // 本月绩效数据
  },

  /**
   * 跳转至佣金帮助
   */
  tapToHelp: function(event) {
    wx.navigateTo({
      url: `/pages/rules/index?ruleType=8`
    })
  },

  // 日绩效
  tapToDay() {
    wx.navigateTo({
      url:'/pages/manager/commision/ranked/index?active=2'
    })
  },

  // 月绩效
  tapToMonth() {
    wx.navigateTo({
      url:'/pages/manager/commision/month/index'
    })
  },

  /**
   * 跳转至佣金订单页面
   */
  tapToOrder: function(event) {
    if (this.data.active == 0) {
      wx.navigateTo({
        url:`/pages/manager/commision/order/index`
      })
    } else {
      wx.navigateTo({
        url:`/pages/manager/commision/ranked/index?active=1`
      })
    }
  },

  /**
   * 跳转至提现明细
   */
  tapToBankroll: function(evnet) {
    wx.navigateTo({
      url: `/pages/manager/commision/bankroll/list/index?active=`+this.data.active
    })
  },

  /**
   * 跳转至提现页面
   */
  tapToTakeOut: function(evnet) {
    //判断用户是否绑定了手机号
    if (!this.data.phone || this.data.phone == 0) {
      this.setData({
        ifName: true
      })
    } else {
      // 是否有佣金，有跳转至佣金页面
      if (this.data.brokerage != 0) {
        wx.navigateTo({
          url:
            `/pages/manager/commision/take-out/index?brokerage=` +
            this.data.brokerage+'&active='+this.data.active
        })
      } else {
        // 没有佣金提示
        wx.showToast({
          title: '暂无可提现金额',
          duration: 2000,
          icon: 'none'
        })
      }
    }

    // wx.showToast({
    //         title: "暂时无法提现",
    //         duration: 1000,
    //         icon: 'none'
    //       })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.active) {
      this.setData({
        active: options.active
      })
    }
  },

  initData: function(active) {
    const that = this
    if (active == 0) {
      rebateModel.getRebate().then(res => {
        const vipUserRebate = res.data.vipUserRebate
          ? res.data.vipUserRebate
          : that.data.vipUserRebate
          that.setData({
          vipUserRebate: vipUserRebate,
          brokerage: res.data.brokerage
        })
      })
    } else {
      rebateModel.queryPerformance().then(res => {
        that.setData({
          Income: res.data,
          brokerage: res.data.usableIncome
        })
      }).catch()
    }

    mineModel
      .getUserInfo()
      .then(res => {
        this.setData({
          phone: res.data.userInfo.userPhone
        })
      })
      .catch()

    const self = this
    wx.login({
      success: function(res) {
        if (res.code) {
          //发起网络请求
          self.setData({
            code: res.code
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
  },

  //取消
  cancel: function() {
    this.setData({
      ifName: false
    })
  },
  confirm: function () {
    this.setData({
      ifName: false 
      }) 
  },

  getPhoneNumber: function(e) {
    const self = this
      
    // var code = wx.getStorageSync('logincode');
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      const params = {
        code: self.data.code,
        encrypteData: e.detail.encryptedData,
        iv: e.detail.iv,
        regType: 3
      }
      mineModel
        .wx_bind_phone(params)
        .then(res => {
          wx.showToast({
            title: '手机号绑定成功',
            duration: 500,
            icon: 'none',
            success: function () {
              if (self.data.brokerage != 0) {
                wx.navigateTo({
                  url: `/pages/manager/commision/take-out/index?brokerage=` +
                    self.data.brokerage
                })
              } else {
                // 没有佣金提示
                wx.showToast({
                  title: '暂无可提现金额',
                  duration: 2000,
                  icon: 'none'
                })
              }
            }
          })
          
        })
        .catch()
    }else{
      wx.showToast({
        title: '未授权',
        icon: 'none',
        success: function () {
          
        }
      })
      return
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.initData(this.data.active)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.initData(this.data.active)
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {}
})
